from django.conf import settings
from django.shortcuts import render
from rest_framework.decorators import action
from rest_framework.mixins import CreateModelMixin, UpdateModelMixin, ListModelMixin
from rest_framework.permissions import IsAuthenticated
from rest_framework.viewsets import GenericViewSet, ViewSet
from rest_framework_jwt.authentication import JSONWebTokenAuthentication

from order.serializer import ProjectOrderCreateModelSerializer, ProjectOrderUpdateModelSerializer
from utils.new_common_response import common_data

from rest_framework.response import Response


# Create your views here.


class ProjectOrderCreateView(CreateModelMixin, GenericViewSet):
    """
    创建评论视图
    """
    serializer_class = ProjectOrderCreateModelSerializer
    authentication_classes = [JSONWebTokenAuthentication]
    permission_classes = [IsAuthenticated]

    def create(self, request, *args, **kwargs):
        # response = super().create(request, *args, **kwargs)
        # old_data = response.data
        # # print(old_data, '>>>>>>>>>>>>')
        #
        # new_data = common_data(old_data)
        # ser = self.get_serializer()
        # order_id = ser.context.get('order_id')
        #
        # print(order_id, '>>>>>>>>>>>>>>')
        #
        # response.data = new_data

        ser = self.get_serializer(data=request.data)
        if ser.is_valid():
            ser.save()
        order_id = ser.context.get('order_id')
        response = ser.data
        # print(response, '>>>>>')
        new_data = common_data(response)
        new_data['order_id'] = order_id
        # print(new_data)
        return Response(new_data)


from order import models


class ProjectOrderUpdateView(UpdateModelMixin, GenericViewSet):
    """
    创建评论视图
    """
    queryset = models.Order.objects.filter(is_delete=False, is_show=True).all()
    serializer_class = ProjectOrderUpdateModelSerializer
    authentication_classes = [JSONWebTokenAuthentication]
    permission_classes = [IsAuthenticated]

    def update(self, request, *args, **kwargs):
        response = super().update(request, *args, **kwargs)
        old_data = response.data

        new_data = common_data(old_data)

        response.data = new_data

        return Response(new_data)


class ProjectOrderAllView(ViewSet):
    """
    ymq
    所有订单
    """

    # queryset = models.Order.objects.filter(is_delete=False, is_show=True).all()
    # serializer_class = ProjectOrderAllModelSerializer

    authentication_classes = [JSONWebTokenAuthentication]
    permission_classes = [IsAuthenticated]

    @action(methods=["GET"], detail=False)
    def order_list(self, request, *args, **kwargs):
        data = self.new_order_list()
        new_data = common_data(data)
        return Response(new_data)

    def new_order_list(self):
        """
        订单
        """

        alreadyOrder = models.Order.objects.filter(is_delete=False, is_show=True).order_by('-id')
        waitPayOrder = models.Order.objects.filter(is_delete=False, is_show=True, order_status=0).order_by('-id')
        lostOrder = models.Order.objects.filter(is_delete=False, is_show=True, order_status=1).order_by('-id')
        """
         [
         { 
         name: "跃动体育运动俱乐部(圆明园店)",
          state: "已取消", 
          time: "2018-10-4 10:00-12:00", 
          status: "未开始",
           url: "../../icon/cp/123.png",
            money: "122" }
            ]

        """
        ret = {
            'alreadyOrder': [
                {'id': item.id, 'name': item.subject, 'state': item.get_order_status_display(),
                 'time': str(item.create_time).replace('T', " ").split('.')[0],
                 'status': item.out_trade_no, 'url': settings.NEW_BASE_URL + item.project.home_img.url,
                 'money': item.total_amount, 'good_id': item.project.id, 'old_state': item.order_status
                 }
                for item in alreadyOrder
            ],
            'waitPayOrder': [
                {'id': item.id, 'name': item.subject, 'state': item.get_order_status_display(),
                 'time': str(item.create_time).replace('T', " ").split('.')[0],
                 'status': item.out_trade_no, 'url': settings.NEW_BASE_URL + item.project.home_img.url,
                 'money': item.total_amount, 'good_id': item.project.id, 'old_state': item.order_status
                 }
                for item in waitPayOrder],
            'lostOrder': [
                {'id': item.id, 'name': item.subject, 'state': item.get_order_status_display(),
                 'time': str(item.create_time).replace('T', " ").split('.')[0],
                 'status': item.out_trade_no, 'url': settings.NEW_BASE_URL + item.project.home_img.url,
                 'money': item.total_amount, 'good_id': item.project.id, 'old_state': item.order_status
                 }
                for item in lostOrder]
        }
        # print(ret)
        return ret
